package com.neusoft.yuejian26.yuejian_26.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.ClubSimpbean;
import com.neusoft.yuejian26.yuejian_26.Bean.MatchSimpbean;
import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.AttentionModel;
import com.neusoft.yuejian26.yuejian_26.model.ClubModel;
import com.neusoft.yuejian26.yuejian_26.model.MatchModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;
import com.squareup.picasso.Picasso;

import retrofit2.Call;

public class MatchSimpActivity extends AppCompatActivity {

    TextView simp_title,simp_match_name,simp_match_time,simp_match_club;
    ImageView match_simp_pic,svg_back,home_match_attention;
    String gid=null;
    String clubid=null;
    String mid=null;
    String uid=null;
    String xid;
    String flag=String.valueOf(4);
    int attentionState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_simp);
        intview();
        intEvent();
        getlastMatchData();
        getMatchSimpData();
    }


    private void getMatchSimpData() {
        MatchModel matchModel = new MatchModel();
        Call call = matchModel.matchSimp(gid);
        matchModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                MatchSimpbean mtsimpdata= (MatchSimpbean) o;
                getClubName(mtsimpdata.getClubid());
                Picasso.with(MatchSimpActivity.this)
                        .load("http://115.159.93.244/new18yuejian/Public/upload/"+mtsimpdata.getGpic())
                        .into(match_simp_pic);
                simp_match_name.setText(mtsimpdata.getGname());
                simp_match_time.setText(mtsimpdata.getGdata());
                getAttentionState();
            }
        });
    }

    private void getClubName(String clubid){
        ClubModel clubModel = new ClubModel();
        Call call = clubModel.clubSimp(clubid);
        clubModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                ClubSimpbean clsimpdata= (ClubSimpbean) o;
                simp_match_club.setText(clsimpdata.getClubname());
            }
        });
    }

    private void getAttentionState(){
        uid = new CacheInfo(MatchSimpActivity.this).getUserIdCache();
        Bundle bundle=this.getIntent().getExtras();
        xid = bundle.getString("matchiid");
        AttentionModel attentionModel = new AttentionModel();
        Call call = attentionModel.checkState(uid,xid,flag);
        attentionModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                Result checkState = (Result) o;
                int state = checkState.getCode();
                attentionState = state;
                if(1==state){
                    home_match_attention.setImageResource(R.drawable.ic_home_attention_unchecked);
                }
                else {
                    home_match_attention.setImageResource(R.drawable.ic_home_attention);
                }
            }
        });
    }

    private class doAttention implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            AttentionModel attentionModel = new AttentionModel();
            Call call = attentionModel.doAttention(uid,xid,flag);
            attentionModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Result doAttention = (Result) o;
                    int states = doAttention.getCode();
                    if (1==states){
                        if (attentionState==1){
                            Toast.makeText(MatchSimpActivity.this,"关注比赛成功",Toast.LENGTH_LONG).show();
                            attentionState = 0;
                            home_match_attention.setImageResource(R.drawable.ic_home_attention);
                        }
                        else {
                            Toast.makeText(MatchSimpActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                            attentionState = 1;
                            home_match_attention.setImageResource(R.drawable.ic_home_attention_unchecked);
                        }
                    }
                }
            });
        }
    }

    private void getlastMatchData(){
        Bundle bundle=this.getIntent().getExtras();
        gid = bundle.getString("matchiid");
        simp_title.setText(bundle.getString("matchname"));
    }

    private  void intview(){
        svg_back =(ImageView) findViewById(R.id.svg_back);
        match_simp_pic=(ImageView) findViewById(R.id.match_simp_pic);
        simp_title=(TextView) findViewById(R.id.simp_title);
        simp_match_name=(TextView) findViewById(R.id.simp_match_name);
        simp_match_time=(TextView) findViewById(R.id.simp_match_time);
        simp_match_club=(TextView) findViewById(R.id.simp_match_club);
        home_match_attention= (ImageView) findViewById(R.id.home_match_attention);
    }

    private void intEvent(){
        home_match_attention.setOnClickListener(new doAttention());
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
